//
//  PayViewController.m
//  moon
//
//  Created by beyself on 16/6/20.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "PayViewController.h"
#import "WXApiRequestHandler.h"

@interface PayViewController ()

@end

@implementation PayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付";
    _mSelected = -1;
    _mWxPay = NO;
    
    int yPos = 10;
    UIView* subview = [[UIView alloc] init];
    subview.backgroundColor = WHITE;
    subview.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 64);
    [self.view addSubview:subview];
    
    UIImageView* imageview = [[UIImageView alloc] init];
    imageview.frame = CGRectMake(10, 10, 44, 44);
    imageview.layer.cornerRadius = 22;
    imageview.layer.masksToBounds = YES;
    imageview.image = [UIImage imageNamed:@"ic_child"];
    [subview addSubview:imageview];
    
    UILabel* label = [[UILabel alloc] init];
    label.frame = CGRectMake(64, 10, SCREEN_WIDTH - 100, 22);
    label.tag = 1000;
    label.textColor = BLUE;
    label.text = _mTitle;
    label.font = FONT_14;
    [subview addSubview:label];
    
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(64, 32, SCREEN_WIDTH - 100, 22);
    label.tag = 1001;
    if(_mFlag == 0) label.textColor = RED;
    else label.textColor = BLUE;
    label.text = _mSubTitle;
    label.font = FONT_14;
    [subview addSubview:label];
    
    _mArray = [[NSMutableArray alloc] init];

    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [Utils sendHttpPost:param method:@"get_finance_packlist" controller:self flag:0];
    
    UIScrollView* scrollview = [[UIScrollView alloc] init];
    scrollview.frame = CGRectMake(0, 84, SCREEN_WIDTH, SCREEN_HEIGHT - 74 - 84);
    scrollview.tag = 10000;
    scrollview.showsVerticalScrollIndicator = NO;
    scrollview.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollview];
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, SCREEN_HEIGHT - 64, SCREEN_WIDTH - 40, 48);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button setBackgroundImage:[Utils imageFromColor:RED] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"支付" forState:UIControlStateNormal];
    [button setTitleColor:WHITE forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showDetail) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void) showDetail
{
    if(((UIButton*)[self.view viewWithTag:4100]).selected)
    {
        [self showAllTextDialog:@"支付宝支付还未开通"];
        return;
    }
    else if(!((UIButton*)[self.view viewWithTag:4101]).selected)
    {
        [self showAllTextDialog:@"请选择支付方式"];
        return;
    }
    
    NSString* member = nil;
    NSString* price = nil;
    
    if(_mSelected == -1)
    {
        [self showAllTextDialog:@"请选择套餐"];
        return;
    }
    
    NSDictionary* item = _mArray[_mSelected];
    member = item[@"name"];
    price = [NSString stringWithFormat:@"%d",(int)([item[@"price"] doubleValue]*100)];
    
    _mWxPay = YES;
    [WXApiRequestHandler sendWXpayWithServer:[NSString stringWithFormat:@"%@(%@元/%@个月)",item[@"name"],[item[@"price"] description],[item[@"months"] description]]
                                       price:[NSString stringWithFormat:@"%d",(int)([item[@"price"] doubleValue]*100)]
                                     student:_childId
                                          tc:[item[@"id"] intValue]
                                      userId:[APPDELEGATE.mMemberInfo[@"id"] intValue]];
}

- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:[Utils readDefaultUser:@"username"] forKey:@"phonenumber"];
    [param setObject:[Utils readDefaultUser:@"password"] forKey:@"password"];
    [Utils sendHttpPost:param method:@"member_login" controller:self flag:1];
}

- (void) viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:@"pay_notify" object:nil];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"pay_notify" object:nil];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    if(flag == 1)
    {
        [Utils saveCustomObject:data[@"member"] key:LOGIN_INFO];
        [APPDELEGATE.mMemberInfo setDictionary:data[@"member"]];
        
        NSArray* child = data[@"member"][@"children"];
        for(NSDictionary* dic in child)
        {
            if([dic[@"student_id"] intValue] == _childId)
            {
                if(![dic[@"expire_date"] isEqualToString:_mExpireDate])
                {
                    [self.navigationController popViewControllerAnimated:YES];
                }
                break;
            }
        }
        
        return;
    }
    
    [_mArray setArray:data[@"data"]];
    
    UIScrollView* scrollview = (UIScrollView*)[self.view viewWithTag:10000];
    
    int yPos = 0;
    for(int i = 0; i < _mArray.count; i++)
    {
        NSDictionary* item = _mArray[i];
        
        UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 4000 + i;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
        [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(classClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"check_win"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"check_win_focus"] forState:UIControlStateSelected];
        [scrollview addSubview:button];
        
        UILabel* label = [[UILabel alloc] init];
        label.frame = CGRectMake(44, 0, SCREEN_WIDTH - 44, 44);
        label.font = FONT_14;
        label.textColor = BLACK;
        label.numberOfLines = 0;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        NSString* text = [NSString stringWithFormat:@"%@(%@元/%@个月)",item[@"name"],[item[@"price"] description],[item[@"months"] description]];
        if(item[@"remark"] && [item[@"remark"] description].length) text = [NSString stringWithFormat:@"%@\n%@",text,item[@"remark"]];
        
        label.text = text;
        int heigt = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 44, 1000)].height + 8;
        if(heigt < 20)
        {
            label.frame = CGRectMake(44, 0, SCREEN_WIDTH - 44, 44);
            button.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 44);
            yPos += 44;
        }
        else
        {
            label.frame = CGRectMake(44, 0, SCREEN_WIDTH - 44, heigt + 12);
            button.frame = CGRectMake(0, yPos, SCREEN_WIDTH, heigt + 12);
            yPos += heigt + 12;
        }
        [button addSubview:label];
        
        CALayer* layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, button.frame.size.height - .5, SCREEN_WIDTH, .5);
        layer.backgroundColor = LINE.CGColor;
        [button.layer addSublayer:layer];
    }
    
    yPos += 10;
    for(int i = 0; i < 2; i++)
    {
        UIButton* button  = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 4100 + i;
        button.frame = CGRectMake(0, 44*i + yPos, SCREEN_WIDTH, 44);
        button.titleLabel.font = FONT_15;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button setTitleColor:BLACK forState:UIControlStateNormal];
        [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateNormal];
        [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        if(i == 0) [button setTitle:@"支付方式：" forState:UIControlStateNormal];
        if(i == 1) [button setTitle:@"微信支付" forState:UIControlStateNormal];
        if(i == 2) [button setTitle:@"支付宝支付" forState:UIControlStateNormal];
        if(i > 0)
        {
            [button setImage:[UIImage imageNamed:@"check_win"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"check_win_focus"] forState:UIControlStateSelected];
        }
        else
        {
            button.enabled = NO;
            [button setTitle:@"支付方式：" forState:UIControlStateDisabled];
            [button setBackgroundImage:[Utils imageFromColor:WHITE] forState:UIControlStateDisabled];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        }
        [scrollview addSubview:button];
        
        CALayer* layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, 43.5, SCREEN_WIDTH, .5);
        layer.backgroundColor = LINE.CGColor;
        [button.layer addSublayer:layer];
    }
    
    scrollview.contentSize = CGSizeMake(0, yPos + 98);
}

- (void) classClick:(UIButton*)btn
{
    _mSelected = (int)btn.tag - 4000;
    for(int i = 0; i < _mArray.count; i++)
    {
        ((UIButton*)[self.view viewWithTag:4000 + i]).selected = NO;
    }
    
    btn.selected = YES;
}

- (void) buttonClick:(UIButton*)btn
{
    ((UIButton*)[self.view viewWithTag:4100]).selected = NO;
    ((UIButton*)[self.view viewWithTag:4101]).selected = NO;
    ((UIButton*)[self.view viewWithTag:4102]).selected = NO;
    btn.selected = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
